<head>
  <style> body { margin: 0; } </style>

  <script type="importmap">{ "imports": {
    "react": "https://esm.sh/react",
    "react-dom": "https://esm.sh/react-dom/client"
  }}</script>

<!--  <script type="module">import * as React from 'react'; window.React = React;</script>-->
<!--  <script src="../../src/packages/react-force-graph-2d/dist/react-force-graph-2d.js" defer></script>-->
</head>

<body>
  <div id="graph"></div>

  <script src="//unpkg.com/@babel/standalone"></script>
  <script type="text/jsx" data-type="module">
    import ForceGraph2D from 'https://esm.sh/react-force-graph-2d?external=react';
    import React from 'react';
    import { createRoot } from 'react-dom';
    import { genRandomTree } from '../datasets/random-data.js';

    function nodePaint({ id, x, y }, color, ctx) {
      ctx.fillStyle = color;
      [
        () => { ctx.fillRect(x - 6, y - 4, 12, 8); }, // rectangle
        () => { ctx.beginPath(); ctx.moveTo(x, y - 5); ctx.lineTo(x - 5, y + 5); ctx.lineTo(x + 5, y + 5); ctx.fill(); }, // triangle
        () => { ctx.beginPath(); ctx.arc(x, y, 5, 0, 2 * Math.PI, false); ctx.fill(); }, // circle
        () => { ctx.font = '10px Sans-Serif'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.fillText('Text', x, y); } // text
      ][id%4]();
    }

    // gen a number persistent color from around the palette
    const getColor = n => '#' + ((n * 1234567) % Math.pow(2, 24)).toString(16).padStart(6, '0');

    createRoot(document.getElementById('graph')).render(
      <ForceGraph2D
        graphData={genRandomTree(20)}
        nodeLabel="id"
        nodeCanvasObject={(node, ctx) => nodePaint(node, getColor(node.id), ctx)}
        nodePointerAreaPaint={nodePaint}
      />
    );
  </script>
</body>